package cmdb

import (
	"errors"
	"k8s-platform/dao"
	"k8s-platform/model"
)

var HostServer hostserver

type hostserver struct{}

// 列表
func (*hostserver) List() ([]*model.HostServer, error) {
	return dao.HostServer.List()
}

// 查询单个
func (*hostserver) Has(hostserver string) (*model.HostServer, bool, error) {
	return dao.HostServer.Has(hostserver)
}

// 查询单个
func (*hostserver) Get(id int64) (*model.HostServer, bool, error) {
	return dao.HostServer.Get(id)
}

// 新增
func (*hostserver) Add(hostserver *model.HostServer, hostgroupID int64, credentialID int64) error {
	_, has, err := dao.HostServer.Has(hostserver.Hostname)
	if err != nil {
		return err
	}
	if has {
		return errors.New("该服务器主机名已存在，请重新添加")
	}

	if err := dao.HostServer.Add(hostserver, hostgroupID, credentialID); err != nil {
		return err
	}
	return nil
}

// 更新
func (*hostserver) Update(hostserver *model.HostServer, updategroupID int64, CredentialID int64) error {
	return dao.HostServer.Update(hostserver, updategroupID, CredentialID)
}

// 同步
func (*hostserver) Sync(hostserver *model.HostServer, updategroupID int64) error {
	return dao.HostServer.Sync(hostserver, updategroupID)
}

// 删除
func (*hostserver) Delete(hostserver *model.HostServer, id int64) error {
	return dao.HostServer.Delete(hostserver, id)
}
